// frontend/js/api.js

/**
 * API client for communicating with the Agentic Jira backend
 */
class ApiClient {
    constructor(baseUrl = 'http://localhost:8000') {
        this.baseUrl = baseUrl;
    }

    /**
     * Send message to the agent
     * @param {string} message - User's message
     * @returns {Promise<Object>} - Agent's response
     */
    async sendMessage(message) {
        try {
            const response = await fetch(`${this.baseUrl}/api/send-message`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ message }),
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            console.log('Message sent:', message);
            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw new Error(`Failed to send message: ${error.message}`);
        }
    }

    /**
     * Test MCP connection
     * @returns {Promise<Object>} - Connection test result
     */
    async testMcpConnection() {
        try {
            const response = await fetch(`${this.baseUrl}/api/test-mcp`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            console.log('MCP connection tested');
            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw new Error(`Failed to test MCP connection: ${error.message}`);
        }
    }

    /**
     * Clear conversation history
     * @returns {Promise<Object>} - Clear confirmation
     */
    async clearConversation() {
        try {
            const response = await fetch(`${this.baseUrl}/api/clear-conversation`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            console.log('Conversation cleared');
            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw new Error(`Failed to clear conversation: ${error.message}`);
        }
    }

    /**
     * Exit application
     * @returns {Promise<Object>} - Exit confirmation
     */
    async exitApplication() {
        try {
            const response = await fetch(`${this.baseUrl}/api/exit`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            console.log('Exit command sent');
            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw new Error(`Failed to exit application: ${error.message}`);
        }
    }
}

// Export for use in other modules
const apiClient = new ApiClient();